#ifndef _Operande_h
#define _Operande_h

#include <string>
#include "IdentTable.hpp"
#include "../inc/Code3ad.hpp"
#include "../src/type/Type.hpp"

using std::stringstream;
using std::cout;
using std::map;
using std::endl;
using std::string;
using std::pair;


class Operande{
public:
	bool type;
	//type=0 : valeur, type=1 : operateur
	int valeur;
	char* codeOp;
	
    Operande(){
        codeOp = new char[1];
    	};
  Operande(c3a & c){
    if(c.type == Type::TYPE_INTEGER){
      initFromInt(c.i);
    }else if(c.type ==Type::TYPE_REAL){
      initFromFloat(c.f);
    }else if(c.type == Type::TYPE_STRING){
      initFromC(c.chet);
    }else if(c.type == Type::TYPE_BOOL){
      initFromBool(c.b);
    }else{
      Operande();
    }
  }
    	Operande(int c) {
    		initFromInt(c);		
    	};
    	
    	Operande(float c) {
        initFromFloat(c);
    	};
    	
    	Operande(bool c) {
        initFromBool(c);
      };
    	
    	Operande(char* c) {
    		initFromC(c);
    	};
  
    	string getOp(/*IdentTable i*/) {
    		if(!type){
    			/*char* s = i.getName(valeur);
    			return(s);*/
    		}
    		else{
    			return(codeOp);
    		}
    	};
    char * getOpAsCharEt(){
        return codeOp;
    }
  void initFromC(char *c){
    type = 1;
    codeOp = c;
  }
  void initFromInt(int c){
    type = 1;
    stringstream sstm;
		sstm << c;
		string s = sstm.str();
    size_t size = s.size() + 1;
    char * buffer = new char[ size ];
    strncpy( buffer, s.c_str(), size );
    codeOp = buffer;
  }
  void initFromFloat(float c){
    type = 1;
    stringstream sstm;
    sstm << c;
    string s = sstm.str();	
    size_t size = s.size() + 1;
    char * buffer = new char[ size ];
    strncpy( buffer, s.c_str(), size );
    codeOp = buffer;
  }
  void initFromBool(bool c){
    type = 1;
    stringstream sstm;
    if(c){
      sstm << "true";
    }else{
      sstm << "false";
    }
    
		string s = sstm.str();
    size_t size = s.size() + 1;
    char * buffer = new char[ size ];
    strncpy( buffer, s.c_str(), size );
    codeOp = buffer;
  }
    	
 
    
    ~Operande(){
    };
    
    Operande& operator=(Operande & o1){
        type = o1.type;
        valeur = o1.valeur;
        
    		codeOp = strdup(o1.codeOp);	
    		return *this;
    	}

};

#endif
